package com.t174.car.service.impl;

import com.t174.car.dao.ComponentsDao;
import com.t174.car.dao.RepairorderComponentsDao;
import com.t174.car.dao.RepairorderDao;
import com.t174.car.dao.RepairorderinfoDao;
import com.t174.car.entity.Repairorder;
import com.t174.car.entity.RepairorderComponents;
import com.t174.car.entity.Repairorderinfo;
import com.t174.car.service.RepairOrder;
import com.t174.car.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RepairOrderImpl implements RepairOrder {
    @Autowired
    RepairorderDao repairorderDao;
    @Autowired
    RepairorderinfoDao repairorderinfoDao;
    @Autowired
    RepairorderComponentsDao rcDao;
    @Autowired
    ComponentsDao componentsDao;
    @Override
    public int addOrder(Repairorder repairOrder,Integer[] rsId) {
        String orId = StringUtils.getOrderNo();//获得订单编号
        repairOrder.setOrId(orId);
        int i = repairorderDao.addOrder(repairOrder);//添加订单
        int oid = repairorderDao.getIdByOr(orId);
        Repairorderinfo repairorderinfo = new Repairorderinfo();
        repairorderinfo.setRoId(oid);
        for(Integer item:rsId){
            repairorderinfo.setRsId(item);
            i = repairorderinfoDao.addOrderInfo(repairorderinfo);//订单详情
        }
        return i;
    }

    @Override
    public int addOCinfo(RepairorderComponents repairorderComponents,Integer[] cid,Integer[] countInt) {
        int count = 0;
        for (int i=0;i<cid.length;i++){
            repairorderComponents.setcId(cid[i]);
            repairorderComponents.setCount(countInt[i]);
            count = rcDao.addRCinfo(repairorderComponents);
            componentsDao.upCount(repairorderComponents.getCount(),repairorderComponents.getcId());
        }
        return count;
    }
}
